/*********************\
* Author:Dzlua
* QQ:505544956
* Time:2017/03/22
\**********************/
#include "SDLuaScript.h"
#include "SDLuaMgr.h"

namespace DuiLib {

SDLuaScript::SDLuaScript(LuaState* luastate /*= nullptr*/)
{
    m_mgr = new SDLuaMgr;
    m_mgr->Init(luastate);
}

SDLuaScript::~SDLuaScript()
{}

//SDIScript::
void SDLuaScript::Quit()
{
    if (!m_mgr) return;
    m_mgr->Quit();
    delete m_mgr;
    m_mgr = nullptr;
}

//SDIScript::
void* SDLuaScript::HandleMessage(SDWindow* wnd, SPDUI_MSG msg, void* data1 /*= 0*/, void* data2 /*= 0*/)
{
    return m_mgr->HandleMessage(wnd, msg, data1, data2);
}

//SDIScript
// called by SDWindow::~OnFinalMessage
void SDLuaScript::RemoveWnd(SDWindow* wnd)
{
    m_mgr->RemoveObj(wnd);
}

//SDIScript
// called by CControlUI::~CControlUI->SDWindow::~OnRemoveControl
void SDLuaScript::RemoveCtrl(SDWindow* wnd, CControlUI* ctrl)
{
    m_mgr->RemoveObj(ctrl);
}

//SDIScript
// called by CDialogBuilder::_Parse->SDWindow::~OnAfterCreateControl
void SDLuaScript::AddCtrl(SDWindow* wnd, CControlUI* ctrl, LPCTSTR ctrlname)
{
    m_mgr->AddObj(SDConvert::TToU(ctrlname).c_str(), ctrl);
}

//SDIScript
// called by SDWindow::SDWindow
void SDLuaScript::AddWnd(SDWindow* wnd, LPCTSTR metaname)
{
    m_mgr->AddObj(SDConvert::TToU(metaname).c_str(), wnd);
}

bool SDLuaScript::DoFile(CDuiString file)
{
    return m_mgr->DoFile(SDConvert::TToU(file.GetData()).c_str());
}

LuaPlus::LuaState* SDLuaScript::GetState() const
{
    return m_mgr->GetState();
}

}